package indi.caibo.service_mail.service.impl;

import indi.caibo.service_mail.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author Bo Cai
 * @version 1.0
 * @date 2021/11/26 16:33
 */
@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;

    @Override
    public boolean sendVerificationCode(String mailUrl, String verificationCode) {
        if (StringUtils.isEmpty(mailUrl)) return false;

        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人,与配置文件中的帐号一致
        message.setFrom("caiboo520@yeah.net");
        // 收件人,有多个收件人时用逗号拼接
        message.setTo("caibo0520@qq.com");
        // 邮件标题
        message.setSubject("【谷粒学院验证码】");
        // 邮件内容
        message.setText("您好!您的验证码是：" + verificationCode + "\n验证码将会在五分钟后失效，请注意使用！");
        // 抄送人，多个收件人用逗号拼接
        try {
            mailSender.send(message);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        }
        return false;
    }
}
